package com.okyai.qa.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.qa.domain.QuestionnaireQuestions;
import com.okyai.qa.service.IQuestionnaireQuestionsService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 问卷题目Controller
 * 
 * @author okyai
 * @date 2021-03-16
 */
@Controller
@RequestMapping("/qa/questions")
public class QuestionnaireQuestionsController extends BaseController
{
    private String prefix = "qa/questions";

    @Autowired
    private IQuestionnaireQuestionsService questionnaireQuestionsService;

    @RequiresPermissions("qa:questions:view")
    @GetMapping()
    public String questions()
    {
        return prefix + "/questions";
    }

    /**
     * 查询问卷题目列表
     */
    @RequiresPermissions("qa:questions:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QuestionnaireQuestions questionnaireQuestions)
    {
        startPage();
        List<QuestionnaireQuestions> list = questionnaireQuestionsService.selectQuestionnaireQuestionsList(questionnaireQuestions);
        return getDataTable(list);
    }

    /**
     * 导出问卷题目列表
     */
    @RequiresPermissions("qa:questions:export")
    @Log(title = "问卷题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuestionnaireQuestions questionnaireQuestions)
    {
        List<QuestionnaireQuestions> list = questionnaireQuestionsService.selectQuestionnaireQuestionsList(questionnaireQuestions);
        ExcelUtil<QuestionnaireQuestions> util = new ExcelUtil<QuestionnaireQuestions>(QuestionnaireQuestions.class);
        return util.exportExcel(list, "questions");
    }

    /**
     * 新增问卷题目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存问卷题目
     */
    @RequiresPermissions("qa:questions:add")
    @Log(title = "问卷题目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuestionnaireQuestions questionnaireQuestions)
    {
        return toAjax(questionnaireQuestionsService.insertQuestionnaireQuestions(questionnaireQuestions));
    }

    /**
     * 修改问卷题目
     */
    @GetMapping("/edit/{questionsId}")
    public String edit(@PathVariable("questionsId") Long questionsId, ModelMap mmap)
    {
        QuestionnaireQuestions questionnaireQuestions = questionnaireQuestionsService.selectQuestionnaireQuestionsById(questionsId);
        mmap.put("questionnaireQuestions", questionnaireQuestions);
        return prefix + "/edit";
    }

    /**
     * 修改保存问卷题目
     */
    @RequiresPermissions("qa:questions:edit")
    @Log(title = "问卷题目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuestionnaireQuestions questionnaireQuestions)
    {
        return toAjax(questionnaireQuestionsService.updateQuestionnaireQuestions(questionnaireQuestions));
    }

    /**
     * 删除问卷题目
     */
    @RequiresPermissions("qa:questions:remove")
    @Log(title = "问卷题目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(questionnaireQuestionsService.deleteQuestionnaireQuestionsByIds(ids));
    }
}
